const AV = require("leanengine");
const BaseService = require("./baseService");

class Service extends BaseService {
  constructor() {
    super("Film");
  }

  async findByCityId(ctx) {
    const { cityId } = ctx.params;
    const city = new AV.Object.createWithoutData("City", cityId);
    const query = new AV.Query("Film");
    query.equalTo("dependent", city);
    const cities = query.find();
    return cities;
  }

  async getDetail(ctx) {
    const { filmId } = ctx.params;
    if (!filmId) {
      return {
        code: "602",
        msg: "filmId不存在"
      };
    }

    try {
      const query = new AV.Query("Film");
      const result = await query.get(filmId);
      const film = {
        ...result.attributes,
        item: undefined,
        videoId: undefined,
        filmType: JSON.parse(result.get("filmType")).name,
        filmId: result.id,
        actors: JSON.parse(result.get("actors"))
      };
      return {
        code: "666",
        film,
        msg: "success"
      };
    } catch (error) {
      return {
        code: error.code,
        msg: error.code === 101 ? "影片不存在" : error.message
      };
    }
  }

  /**
   * page当前页
   * type电影类型1=>正在播放,2=>即将播放
   */
  async getList(ctx) {
    try {
      const { pageNum = 1, pageSize = 5, type = 1 } = ctx.params;
      const query = new AV.Query("Film");
      // type=1 正在播放 2即将播放
      if (type == 2) {
        query.equalTo("isSale", "false");
      } else {
        query.equalTo("isSale", "true");
      }
      const count = await query.count();
      query.ascending("premiereAt");
      // 降序排列
      // query.descending("premiereAt");
      // 分页
      query.limit(pageSize); // 最多返回 10 条结果
      query.skip((pageNum - 1) * pageSize); // 跳过 20 条结果
      const totalPage = Math.ceil(count / pageSize);
      let result = await query.find();

      const films = result.map(item => {
        return {
          ...item.attributes,
          item: undefined,
          timeType: undefined,
          actors: undefined,
          videoId: undefined,
          filmId: item.id,
          filmType: JSON.parse(item.get("filmType")).name,
          actorStr: this.madeActorStr(item.get("actors"))
        };
      });

      // 添加banner
      let banners = await this.getBanners();
      return {
        code: "666",
        pageNum,
        pageSize,
        totalPage,
        msg: "success",
        banners,
        films
      };
    } catch (error) {
      console.log(error);
      return {
        code: "602",
        ...error
      };
    }
  }

  // 获取banner图片列表
  getBanners() {
    return new Promise(async (resolve, reject) => {
      try {
        let query = new AV.Query("Banner");
        let result = await query.find();
        let newList = result.map(item => {
          return {
            ...item.attributes,
            id: item.id
          };
        });
        resolve(newList);
      } catch (error) {
        reject(error);
      }
    });
  }

  // 把演员数组拼接一下字符串
  madeActorStr(actors) {
    actors = JSON.parse(actors);
    let names = actors.map(item => {
      return item.name;
    });
    return names.join(",");
  }
}

module.exports = new Service();
